package com.irdstudio.cdp.pboc.service.impl;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.cdp.pboc.service.dao.CreditQueryDetailDao;
import com.irdstudio.cdp.pboc.service.domain.CreditQueryDetail;
import com.irdstudio.cdp.pboc.service.facade.CreditQueryDetailService;
import com.irdstudio.cdp.pboc.service.facade.PbocCreditqueryDetailService;
import com.irdstudio.cdp.pboc.service.vo.CreditQueryDetailVO;
import com.irdstudio.cdp.pboc.service.vo.PbocCreditqueryDetailVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service("creditQueryDetailService")
public class CreditQueryDetailServiceImpl implements CreditQueryDetailService, FrameworkService{

	private static Logger logger = LoggerFactory.getLogger(CreditQueryDetailServiceImpl.class);
	
	@Autowired
	private CreditQueryDetailDao creditQueryDetailDao;
	@Autowired
	private PbocCreditqueryDetailService pbocCreditqueryDetailService;
	
	@Override
	public List<CreditQueryDetailVO> queryAllOwner(CreditQueryDetailVO creditQueryDetailVO) {
		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<CreditQueryDetail> creditQueryDetails = creditQueryDetailDao.queryAllOwnerByPage(creditQueryDetailVO);
		logger.debug("当前查询本人所属数据信息的结果集数量为:" + creditQueryDetails.size());
		List<CreditQueryDetailVO> list = new ArrayList<CreditQueryDetailVO>();
		try {
			pageSet(creditQueryDetails, creditQueryDetailVO);
			handleRs(list, creditQueryDetails);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("数据转换出现异常!", e);
		}
		return list;

	}

	@Override
	public List<CreditQueryDetailVO> queryAllCurrOrg(CreditQueryDetailVO creditQueryDetailVO) {
		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<CreditQueryDetail> creditQueryDetails = creditQueryDetailDao.queryAllCurrOrgByPage(creditQueryDetailVO);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + creditQueryDetails.size());
		List<CreditQueryDetailVO> list = new ArrayList<CreditQueryDetailVO>();
		try {
			pageSet(creditQueryDetails, creditQueryDetailVO);
			handleRs(list, creditQueryDetails);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<CreditQueryDetailVO> queryAllCurrDownOrg(CreditQueryDetailVO creditQueryDetailVO) {
		logger.debug("当前查询本人所属机构及下属机构数据信息的参数信息为:");
		List<CreditQueryDetail> creditQueryDetails = creditQueryDetailDao.queryAllCurrDownOrgByPage(creditQueryDetailVO);
		logger.debug("当前查询本人所属机构及下属机构数据信息的结果集数量为:" + creditQueryDetails.size());
		List<CreditQueryDetailVO> list = new ArrayList<CreditQueryDetailVO>();
		try {
			pageSet(creditQueryDetails, creditQueryDetailVO);
			handleRs(list, creditQueryDetails);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<CreditQueryDetailVO> queryAllCurrOwnerPrd(CreditQueryDetailVO creditQueryDetailVO) {
		logger.debug("当前产品种类下数据信息的参数信息为:");
		List<CreditQueryDetail> creditQueryDetails = creditQueryDetailDao.queryAllCurrOwnerPrdByPage(creditQueryDetailVO);
		logger.debug("当前产品种类下属机构数据信息的结果集数量为:" + creditQueryDetails.size());
		List<CreditQueryDetailVO> list = new ArrayList<CreditQueryDetailVO>();
		try {
			pageSet(creditQueryDetails, creditQueryDetailVO);
			handleRs(list, creditQueryDetails);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<CreditQueryDetailVO> queryAllData(CreditQueryDetailVO creditQueryDetailVO) {
		logger.info("开始查询征信查询明细！");
		List<CreditQueryDetailVO> list = new ArrayList<CreditQueryDetailVO>();
		try {
			if(creditQueryDetailVO.getTime_b2e() != null && creditQueryDetailVO.getTime_b2e().toString() !="[]") {
				logger.info(creditQueryDetailVO.getTime_b2e().toString());
				creditQueryDetailVO.setStrTime_b(creditQueryDetailVO.getTime_b2e().get(0));
				creditQueryDetailVO.setEndTime_e(creditQueryDetailVO.getTime_b2e().get(1));
			}
			List<CreditQueryDetail> queryRsCreditQueryDetails = creditQueryDetailDao.queryAllDataByCondition(creditQueryDetailVO);
			handleRs(list, queryRsCreditQueryDetails);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<CreditQueryDetailVO> queryAllOwnerExp(CreditQueryDetailVO creditQueryDetailVO) {
		logger.debug("当前导出本人所属数据信息的参数信息为:");
		List<CreditQueryDetailVO> list = new ArrayList<CreditQueryDetailVO>();
		try {
			if(creditQueryDetailVO.getTime_b2e() != null && creditQueryDetailVO.getTime_b2e().toString() !="[]") {
				logger.info(creditQueryDetailVO.getTime_b2e().toString());
				creditQueryDetailVO.setStrTime_b(creditQueryDetailVO.getTime_b2e().get(0));
				creditQueryDetailVO.setEndTime_e(creditQueryDetailVO.getTime_b2e().get(1));
			}
			List<CreditQueryDetail> creditQueryDetails = creditQueryDetailDao.queryAllOwnerExp(creditQueryDetailVO);
			handleRs(list, creditQueryDetails);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<CreditQueryDetailVO> queryAllCurrOrgExp(CreditQueryDetailVO creditQueryDetailVO) {
		logger.debug("当前导出本人所属机构数据信息的参数信息为:");
		List<CreditQueryDetailVO> list = new ArrayList<CreditQueryDetailVO>();
		try {
			if(creditQueryDetailVO.getTime_b2e() != null && creditQueryDetailVO.getTime_b2e().toString() !="[]") {
				logger.info(creditQueryDetailVO.getTime_b2e().toString());
				creditQueryDetailVO.setStrTime_b(creditQueryDetailVO.getTime_b2e().get(0));
				creditQueryDetailVO.setEndTime_e(creditQueryDetailVO.getTime_b2e().get(1));
			}
			List<CreditQueryDetail> creditQueryDetails = creditQueryDetailDao.queryAllCurrOrgExp(creditQueryDetailVO);
			handleRs(list, creditQueryDetails);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<CreditQueryDetailVO> queryAllCurrDownOrgExp(CreditQueryDetailVO creditQueryDetailVO) {
		logger.debug("当前导出本人所属机构及下属机构数据信息的参数信息为:");
		List<CreditQueryDetailVO> list = new ArrayList<CreditQueryDetailVO>();
		try {
			if(creditQueryDetailVO.getTime_b2e() != null && creditQueryDetailVO.getTime_b2e().toString() !="[]") {
				logger.info(creditQueryDetailVO.getTime_b2e().toString());
				creditQueryDetailVO.setStrTime_b(creditQueryDetailVO.getTime_b2e().get(0));
				creditQueryDetailVO.setEndTime_e(creditQueryDetailVO.getTime_b2e().get(1));
			}
			List<CreditQueryDetail> creditQueryDetails = creditQueryDetailDao.queryAllCurrDownOrgExp(creditQueryDetailVO);
			handleRs(list, creditQueryDetails);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<CreditQueryDetailVO> queryAllCurrOwnerPrdExp(CreditQueryDetailVO creditQueryDetailVO) {
		logger.debug("当前导出产品种类下属机构数据信息的参数信息为:");
		List<CreditQueryDetailVO> list = new ArrayList<CreditQueryDetailVO>();
		try {
			if(creditQueryDetailVO.getTime_b2e() != null && creditQueryDetailVO.getTime_b2e().toString() !="[]") {
				logger.info(creditQueryDetailVO.getTime_b2e().toString());
				creditQueryDetailVO.setStrTime_b(creditQueryDetailVO.getTime_b2e().get(0));
				creditQueryDetailVO.setEndTime_e(creditQueryDetailVO.getTime_b2e().get(1));
			}
			List<CreditQueryDetail> creditQueryDetails = creditQueryDetailDao.queryAllCurrOwnerPrdExp(creditQueryDetailVO);
			handleRs(list, creditQueryDetails);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	private void handleRs(List<CreditQueryDetailVO> list, List<CreditQueryDetail> queryRsCreditQueryDetails) throws Exception {
		if(Objects.nonNull(queryRsCreditQueryDetails)) {
			for (CreditQueryDetail item : queryRsCreditQueryDetails) {
				CreditQueryDetailVO infoVO = new CreditQueryDetailVO();
				beanCopy(item, infoVO);
				
				PbocCreditqueryDetailVO pbocCreditqueryDetailVO = new PbocCreditqueryDetailVO();
				pbocCreditqueryDetailVO.setReportId(item.getReportId());
				PbocCreditqueryDetailVO queryRs = pbocCreditqueryDetailService.queryAllByReportId(pbocCreditqueryDetailVO);
				if(Objects.nonNull(queryRs)) {
					infoVO.setQueryReason(queryRs.getQueryReason());
					infoVO.setQueryUser(queryRs.getQueryUser());
					infoVO.setQueryDate(queryRs.getQueryDate());
				}
				list.add(infoVO);
			}
		}
		
	}

	@Override
	public int insertCreditQueryDetailVO(CreditQueryDetailVO creditQueryDetailVO) {
		logger.debug("当前新增数据为:" + creditQueryDetailVO.toString());
		int num = 0;
		try {
			CreditQueryDetail creditQueryDetail = new CreditQueryDetail();
			beanCopy(creditQueryDetailVO, creditQueryDetail);
			num = creditQueryDetailDao.insertCreditQueryDetail(creditQueryDetail);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	@Override
	public List<CreditQueryDetailVO> queryReportId(CreditQueryDetailVO creditQueryDetailVO) {
		logger.debug("当前查询条件数据为:" + creditQueryDetailVO.toString());
		List<CreditQueryDetailVO> list = null;
		try {
			CreditQueryDetail creditQueryDetail = new CreditQueryDetail();
			beanCopy(creditQueryDetailVO, creditQueryDetail);
			List<CreditQueryDetail> queryRs = creditQueryDetailDao.queryReportId(creditQueryDetail);
			if (Objects.nonNull(queryRs)) {
				list = (List<CreditQueryDetailVO>) beansCopy(queryRs,creditQueryDetailVO.getClass());
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
